﻿using Abp.Application.Services.Dto;
using Abp.Web.Models;
using Abp.Web.Mvc.Authorization;
using KDS.Mes.Authorization;
using KDS.Mes.Mes.AlcCodes;
using KDS.Mes.Mes.AlcCodes.Dtos;
using KDS.Mes.Mes.Cartypes;
using KDS.Mes.Mes.Cartypes.Dtos;
using KDS.Mes.Mes.Styles;
using KDS.Mes.Mes.Styles.Dtos;
using KDS.Mes.Web.Models.AlcCodes;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace KDS.Mes.Web.Controllers
{
    public class AlcCodesController : MesControllerBase
    {
        private readonly IAlcCodeAppService _alcCodeAppService;
        private readonly ICartypeAppService _cartypeAppService;
        private readonly IStyleAppService _styleAppService;

        public AlcCodesController(
            IAlcCodeAppService alcCodeAppService,
            ICartypeAppService cartypeAppService,
            IStyleAppService styleAppService
            )
        {
            _alcCodeAppService = alcCodeAppService;
            _cartypeAppService = cartypeAppService;
            _styleAppService = styleAppService;
        }

        public async Task<ActionResult> Index()
        {
            //var model = new GetAlcCodeInput { FilterText = Request.QueryString["filterText"] };
            var cartypes = new List<ComboboxItemDto>();
            var getCartypesOutput = await _cartypeAppService.GetCartypes(new GetCartypeInput());
            cartypes = getCartypesOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.CartypeName)).ToList();

            var styles = new List<ComboboxItemDto>();
            var getStylesOutput = await _styleAppService.Getstyles(new GetStyleInput());
            styles = getStylesOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.StyleName)).ToList();

            cartypes.Insert(0, new ComboboxItemDto("", ""));
            styles.Insert(0, new ComboboxItemDto("", ""));
            var model = new AlcCodesViewModel
            {
                FilterText = Request.QueryString["filterText"],
                Cartypes = cartypes,
                Styles = styles
            };

            return View(model);
        }

        [HttpGet]
        [DontWrapResult]
        public JsonResult GetAllAlcCodeByStyleId(int? id)
        {
            List<ComboboxItemDto> lst = new List<ComboboxItemDto>();
            NullableIdDto<int> input = new NullableIdDto<int>();
            input.Id = id;
            lst = _alcCodeAppService.GetAllAlcCodeByStyleId(input);
            lst.Insert(0, new ComboboxItemDto("", ""));
            return Json(lst, JsonRequestBehavior.AllowGet);
        }




        [DontWrapResult]
        public JsonResult GetAllAlcCodes(
            int limit, int offset, string sortfiled, string sortway,
            string cartypeId,string styleId,string alccode)
        {
            var sort = !string.IsNullOrEmpty(sortfiled) ? string.Format("{0} {1}", sortfiled, sortway) : "";
            var filter = new GetAlcCodeInput
            {
                SkipCount = offset,
                MaxResultCount = limit,
                Sorting = sort,
                CartypeId = cartypeId,
                StyleId= styleId,
                AlcCode=alccode
            };
            var pagedTasks = _alcCodeAppService.GetPagedAlcCodes(filter);
            return Json(new { total = pagedTasks.TotalCount, rows = pagedTasks.Items }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 根据id获取进行编辑或者添加的用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AbpMvcAuthorize(AppPermissions.Pages_Bom_AlcCodes_Create, AppPermissions.Pages_Bom_AlcCodes_Edit)]
        public async Task<PartialViewResult> CreateOrEditAlcCodeModal(int? id)
        {
            var input = new NullableIdDto<int> { Id = id };

            var output = await _alcCodeAppService.GetAlcCodeForEditAsync(input);

            var viewModel = new CreateOrEditAlcCodeModalViewModel(output);

            return PartialView("_CreateOrEditAlcCodeModal", viewModel);
        }
    }
}